package com.divemotor.ventarepuestosinternet.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.primefaces.model.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.divemotor.ventarepuestosinternet.dao.ClienteDAO;
import com.divemotor.ventarepuestosinternet.model.Cliente;

@Repository(value="clienteDAO")
public class ClienteDAOImpl  implements ClienteDAO, Serializable{

	private static final long serialVersionUID = 1L;
	
	@Autowired
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Cliente> listarTodos() {		
		Criteria criteria = sessionFactory
				.getCurrentSession()
				.createCriteria(Cliente.class);
		criteria.add(Restrictions.not(Restrictions.isNull(("idCliente"))));
		
		return criteria.list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Cliente> listarClientes(Cliente cliente) {	
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Cliente.class);
		
		if(!cliente.getRuc().equals("")){
			criteria.add(Restrictions.like("ruc","%"+cliente.getRuc()+"%").ignoreCase());
		}
		
		if(!cliente.getDni().equals("")){
			criteria.add(Restrictions.like("dni","%"+cliente.getDni()+"%").ignoreCase());
		}
		
		if(!cliente.getNombre().equals("")){
			criteria.add(Restrictions.like("nombre","%"+cliente.getNombre()+"%").ignoreCase());
		}
		
		return criteria.list();
	}


	@Override
	public Integer cantidadClientes() {
		Number result = (Number) sessionFactory.getCurrentSession().createQuery("select COUNT(p) from Cliente p").uniqueResult();
	   		return result.intValue() ;
	}

	@Override
	public Integer cantidadClientes(Cliente clienteBuscar) {
	      Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Cliente.class);
			 
	        criteria.setProjection(Projections.count("idCliente"));
	      
			if(!clienteBuscar.getRuc().equals("")){
				criteria.add(Restrictions.like("ruc","%"+clienteBuscar.getRuc()+"%").ignoreCase());
			}
			
			if(!clienteBuscar.getDni().equals("")){
				criteria.add(Restrictions.like("dni","%"+clienteBuscar.getDni()+"%").ignoreCase());
			}
			
			if(!clienteBuscar.getNombre().equals("")){
				criteria.add(Restrictions.like("nombre","%"+clienteBuscar.getNombre()+"%").ignoreCase());
			}
			
			Number result = (Number) criteria.uniqueResult();
		
 	
		return result.intValue();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Cliente> listar(int first, int pageSize, SortOrder sortOrder,
			String sortField) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
				Cliente.class, "cliente");
		criteria.add(Restrictions.not(Restrictions
				.isNull(("cliente.idCliente"))));
		criteria.setFirstResult(first);
		criteria.setMaxResults(pageSize);
		if (!(sortField == null)) {
			if (sortOrder.toString().equals("ASCENDING"))
				criteria.addOrder(Order.asc(sortField.toString()));
			else {
				criteria.addOrder(Order.desc(sortField.toString()));
			}
		}else {
			criteria.addOrder(Order.asc("nombre"));
		}
		return criteria.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Cliente> buscar(int first, int pageSize,Cliente clienteBuscar, SortOrder sortOrder,String sortedField) {
		  Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Cliente.class,"cliente");
			 

			if(!clienteBuscar.getRuc().equals("")){
				criteria.add(Restrictions.like("cliente.ruc","%"+clienteBuscar.getRuc()+"%").ignoreCase());
			}
			
			if(!clienteBuscar.getDni().equals("")){
				criteria.add(Restrictions.like("cliente.dni","%"+clienteBuscar.getDni()+"%").ignoreCase());
			}
			
			if(!clienteBuscar.getNombre().equals("")){
				criteria.add(Restrictions.like("cliente.nombre","%"+clienteBuscar.getNombre()+"%").ignoreCase());
			}
			criteria.add(Restrictions.not(Restrictions.isNull(("cliente.idCliente"))));
			criteria.setFirstResult(first);
			criteria.setMaxResults(pageSize);
	
			if (!(sortedField == null)){
				if (sortOrder.toString().equals("ASCENDING"))
					criteria.addOrder(Order.asc(sortedField.toString()));
					else{
						criteria.addOrder(Order.desc(sortedField.toString()));
					}	
			}else{
				criteria.addOrder(Order.asc("nombre"));
			}
		
			return criteria.list();
	}
}
